package com.sinodata.bsm.cicp.collector.informix;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.constants.SeparatorConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * 由于informix指标修改该采集类放弃使用
 * 
 * Description: 当前处理事务列表采集器
 * 
 * 在成功安装informix数据库的主机上执行:onstat –g act，可以显示当前正在处理的事务
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-18 PM 8:50:11    wangpeng       1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class RunningTransListCollector extends Collector {
    private static Logger logger = Logger.getLogger(RunningTransListCollector.class);
    private static String regex = "(\\d+)\\s+(.*)\\s+(.*)\\s+(.*)\\s+(.*)\\s+(.*)";

    @Override
    public String[] getPropMeta() {
        return new String[] { "RunningTransList" };
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "cmd" };
    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String[] propMetas = this.getPropMeta();
        String cmd = this.getParamMap().get("cmd");
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        ResPropertyValue resPropertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] props = new ResPropertyValue.PropValue[propMetas.length];
        BufferedReader reader = null;
        try {
            reader = CommandReader.getReader(ip, cmd, 0, 0);
            String strLine = null;
            StringBuilder sb = new StringBuilder();
            while ((strLine = reader.readLine()) != null) {
                if (strLine.trim().matches(regex)) {
                    String[] str = strLine.trim().split("\\s+");
                    int i = 0;
                    for (; i < str.length - 1; i++) {
                        sb.append(str[i]).append(SeparatorConstants.COL_SEPARATOR);
                    }
                    sb.append(str[i]).append(SeparatorConstants.LIN_SEPARATOR);
                }
            }
            props[0] = ResPropertyValue.createPropValue(sb.toString(), propMetas[0]);
            resPropertyValueX.setValues(props);
            values.add(resPropertyValueX);
        } catch (IOException e) {
            logger.error("failed to collect informix running translist", e);
            return null;
        }
        return values;
    }
}
